import 'package:flutter/material.dart';
import 'package:flutter_launcher_icons/xml_templates.dart';
import 'package:god_logistics/pages/tabbar.dart';
import 'package:god_logistics/utils/http.dart';
import 'package:god_logistics/utils/showToast.dart';
import 'package:god_logistics/utils/tokenManage.dart';

class Loginpage extends StatefulWidget {
  const Loginpage({super.key});

  @override
  State<Loginpage> createState() => _LoginpageState();
}

class _LoginpageState extends State<Loginpage> {
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  // 登录按钮方法
  void loginButton() async {
    if (_phoneController.text.isEmpty || _passwordController.text.isEmpty) {
      ToastUtil.showInfo('请输入账号和密码');
      return;
    }

    // 登录
    var res = await http.post('/driver/login/account', {
      'account': _phoneController.text,
      'password': _passwordController.text,
    });

    if (res['code'] != 200) return ToastUtil.showInfo('登录失败');
    TokenManager().saveToken(res['data']);
    ToastUtil.showInfo('登录成功' + res['data']);
    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (context) => tabpage()),
      (route) => false,
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _phoneController.text = 'wyh123456';
    _passwordController.text = '123456';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          padding: EdgeInsets.only(top: 50, left: 10, right: 10),
          child: Column(
            children: [
              // 标题
              Text(
                '神领快递',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w800,
                  color: Colors.black,
                ),
              ),
              SizedBox(height: 20),
              // 手机号输入框
              TextField(
                controller: _phoneController,
                keyboardType: TextInputType.phone,
                maxLength: 11,
                decoration: InputDecoration(
                  labelText: '手机号',
                  hintText: '请输入手机号',
                ),
              ),
              SizedBox(height: 20),
              // 密码输入框
              TextField(
                controller: _passwordController,
                keyboardType: TextInputType.text,
                obscureText: true,
                decoration: InputDecoration(labelText: '密码', hintText: '请输入密码'),
              ),
              SizedBox(height: 30),
              ElevatedButton(
                onPressed: () {
                  // TODO  登录
                  loginButton();
                },
                child: Container(
                  width: MediaQuery.of(context).size.width * 0.8,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Text('登录', textAlign: TextAlign.center),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
